1
Input -- BOX -- Output
Input: arguments, global variables
Output: return, print, file(file storage in hardware)

2
Composition:
Call one function in another function

3
Boolean:
True and False
ex: a==b

4
Counting the digit of a number:

1
def num_digits(n):
    count = 0
    while n != 0:
        count = count + 1
        n = n // 10
    return count


2

def num_digits(thenumber):
    tmpstring = str(thenumber)
    print(len(tmpstring))


5
To count how many 5 and 0 in the number
def num_zero_and_five_digits(n):
    count = 0
    while n > 0:
        digit = n % 10
        if digit == 0 or digit == 5:
            count = count + 1
        n = n // 10
    return count


6
In 4.2, tmpstring = str(thenumber) so if we want to count count many words in a number there is an easy way for example:
print(tmpstring.count("3"))


7
How to open python document:
For Mac: 应用程序--python --python documentation

8
How to leave space for integer/string/float:
%d--integer
%f--float
%s--string

ex:
print("%d * %d = %d"%(i,j,i*j),end="\t\t")


9
tips:
\t = tab
\n = newline

10
Print a multiplication table by using composition:

def print_oneline(i):
    for j in range(1,i+1):
        print("%d * %d = %d"%(i,j,i*j),end="\t\t")
 
def print_multiplicationTable(n):
    for i in range(1,n+1):
        print_oneline(i)
        print()
 
 
 

11
program implements a simple guessing game:
import random                   # We cover random numbers in the
rng = random.Random()           #  modules chapter, so peek ahead.
number = rng.randrange(1, 1000) # Get random number between [1 and 1000).
 
guesses = 0
msg = ""
 
while True:
    guess = int(input(msg + "\nGuess my number between 1 and 1000: "))
    guesses += 1
    if guess > number:
        msg += str(guess) + " is too high.\n"
    elif guess < number:
        msg += str(guess) + " is too low.\n"
    else:
        break
 
input("\n\nGreat, you got it in {0} guesses!\n\n".format(guesses))


12

dichotomie:

This divide the range in half and then divide the half into half and repeat this process until we get the number we want.

(the fastest way to find a number in a range)